package com.cj.servlet;

import com.cj.entity.Admin;
import com.cj.servcie.AdminService;
import com.cj.servcie.impl.AdminServiceImpl;
import com.cj.utils.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 系统控制器（Servlet）
 * 登录：
 *    1、登录页面 login.jsp  =》login.html
 *       账号：account    非空
 *       密码：password   非空
 *    2、向servlet发送请求，servlet获取请求中的数据
 *        账号：account
 *        密码：password
 *        2.1 根据 account 跟 password 去 adminDaoImpl 查询该用户是否存在
 *           如果存在，将用户信息存入浏览器
 *              存入浏览器有两中
 *                ① 存在Cookie中
 *                ② 存在session中
 *
 *           如果不存在，返回错误信息
 *
 *       2.2 要进入管理系统，必须有过滤器
 *
 *
 *
 *
 */
@WebServlet("/system")
public class SystemServlet extends HttpServlet {

    //导入管理员service对象
    private AdminService adminService = new AdminServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        System.out.println("已经入systemServlet方法。。。");
        String method = request.getParameter("method");
        switch (method){
            case "login":
                //登录方法
                login(request, response);
                break;
            case "logout":
                //退出
                logout(request, response);
                break;
        }
    }

    /**
     * 管理员登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        System.out.println("已进入登录方法...");
        String account = request.getParameter("account").trim();//.trim()前后空格
        String password = request.getParameter("password").trim();

        if(account == null || account.equals("")){
            request.setAttribute("errAccountMsg", "账号不能为空！");
            //请求转发到登录页
            request.getRequestDispatcher("login.jsp").forward(request,response);
            return;
        }
        if(password == null || password.equals("")){
            request.setAttribute("errPwdMsg", "密码不能为空！");
            request.getRequestDispatcher("login.jsp").forward(request,response);
            return;
        }
        //调用service层方法，同时把密码加密
        Admin admin = adminService.login(account, MD5Util.encrypt(password));

        if(admin == null){
            request.setAttribute("errPwdMsg", "用户名不存在或密码错误！");
            request.getRequestDispatcher("login.jsp").forward(request,response);
            return;
        }
        //创建session对象
        HttpSession session = request.getSession();
        session.setAttribute("admin", admin);
        //重定向到员工列表管理页面
        response.sendRedirect("employee?method=empList");
    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.getSession().invalidate();
        response.sendRedirect("login.jsp");
    }
}
